• Projects
  • R Packages
  • Publications
  • Graduate Students
  • Links
  • Courses
    • Single-case regression analzes
  • Private blog

On this page

  • Hi Wolfgang and Michel, here is a simple example to show the differences between treatment and effect contrasts.
  • Example dataset
  • Descriptives
  • Contrasts

Treatment vs. effect contrasts

regression
statistics
contrasts
Author

Jürgen Wilbert

Published

May 17, 2023

Hi Wolfgang and Michel, here is a simple example to show the differences between treatment and effect contrasts.

Example dataset

Create a random dataset with criteria y, predictors x1, x2 and gender.

All variables are correlated.

set.seed(1234)
n <- 10000
gender <- rep(0:1, each = n/2)
y <- sample(0:10, n, replace = TRUE) + gender * sample(0:10, n, replace = TRUE)
x1 <- sample(0:10, n, replace = TRUE) + gender * y
x2 <- x1 + sample(0:10, n, replace = TRUE) + gender * y

y <- y + (x1 > median(x1) & x2 > median(x2) & gender == 1) * sample(0:10, n, replace = TRUE)

dat <- data.frame(y = y, x1 = x1, x2 = x2, gender = gender)

Descriptives

psych::corr.test(dat)
Call:psych::corr.test(x = dat)
Correlation matrix 
          y   x1   x2 gender
y      1.00 0.79 0.82   0.66
x1     0.79 1.00 0.94   0.74
x2     0.82 0.94 1.00   0.79
gender 0.66 0.74 0.79   1.00
Sample Size 
[1] 10000
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
       y x1 x2 gender
y      0  0  0      0
x1     0  0  0      0
x2     0  0  0      0
gender 0  0  0      0

 To see confidence intervals of the correlations, print with the short=FALSE option
psych::describe(dat)
vars n mean sd median trimmed mad min max range skew kurtosis se
y 1 10000 9.5544 6.773613 8.0 9.044000 7.4130 0 30 30 0.6076269 -0.5395695 0.0677361
x1 2 10000 10.0063 6.672279 9.0 9.585875 7.4130 0 30 30 0.5162774 -0.5350028 0.0667228
x2 3 10000 19.9998 12.575874 16.0 19.046000 11.8608 0 58 58 0.5910003 -0.6639372 0.1257587
gender 4 10000 0.5000 0.500025 0.5 0.500000 0.7413 0 1 1 0.0000000 -2.0002000 0.0050003

Contrasts

The left part of the table is with gender as treatment contrast (0 vs. 1) and the right part with gender as effect contrast (-1 vs. 1)

# Gender has values 0 vs. 1 (treatment contrast)
fit1 <- lm(y ~ gender * x1 * x2, data = dat)

# Gender hast -1 vs. 1 (effect contrast)
dat$gender <- car::recode(dat$gender, "0 = -1; 1 = 1")

fit2 <- lm(y ~ gender * x1 * x2, data = dat)

sjPlot::tab_model(fit1, fit2, show.se = TRUE, show.ci = FALSE, col.order = c("est", "se", "std.est", "p"), digits = 4)
  y y
Predictors Estimates std. Error p Estimates std. Error p
(Intercept) 5.1849 0.1890 <0.001 -0.8279 0.1892 <0.001
gender -12.0257 0.3784 <0.001 -6.0128 0.1892 <0.001
x1 0.0226 0.0422 0.592 0.3045 0.0261 <0.001
x2 -0.0066 0.0240 0.782 0.3072 0.0145 <0.001
gender × x1 0.5637 0.0521 <0.001 0.2819 0.0261 <0.001
gender × x2 0.6277 0.0289 <0.001 0.3139 0.0145 <0.001
x1 × x2 -0.0026 0.0036 0.482 -0.0078 0.0019 <0.001
(gender × x1) × x2 -0.0105 0.0037 0.005 -0.0052 0.0019 0.005
Observations 10000 10000
R2 / R2 adjusted 0.745 / 0.745 0.745 / 0.745
 
Copyright 2023, Jürgen Wilbert